package com.wisezone.workflow.web;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.wisezone.common.annotation.WzFunAnnotation;
import com.wisezone.common.annotation.WzWebAnnotation;
import com.wisezone.common.utils.ConstantUtils;
import com.wisezone.workflow.bean.WfDataDictionary;
import com.wisezone.workflow.bean.WfProcessDefinition;
import com.wisezone.workflow.bean.WfProcessInstance;
import com.wisezone.workflow.bean.WfVariableInstance;
import com.wisezone.workflow.bean.form.WfActivityInstanceForm;
import com.wisezone.workflow.bean.form.WfProcessInstanceForm;
import com.wisezone.workflow.bean.search.WfProcessInstanceSearch;
import com.wisezone.workflow.bean.search.WfVariableInstanceSearch;
import com.wisezone.workflow.core.ProcessInstance;
import com.wisezone.workflow.exception.WfWebException;

/**
 * 流程实例控制类 2010-08-26
 * @author shan
 *
 */
@WzWebAnnotation(moduleName="processInst")
public class WfProcessInstController extends WfWorkFlowController<WfProcessInstance,
WfProcessInstanceSearch,
WfProcessInstanceForm> {

	
	
	private List<WfProcessDefinition> processDefList;
	
	private List<WfDataDictionary> stateList;
	

	 /**
	  * 获得列表
	  * @return
	  * @throws Exception
	  */
	 public String list() throws WfWebException{
		 
		 searchEntity = getSearchEntity(true);
		 pagination = flowEngine.getWfProcessInstanceDao().listFormByPage(searchEntity);
		 return LIST;
	 }

	 
	 /**
	  * 进入新增
	  * @return
	  * @throws Exception
	  */
	 public String enterNew() throws WfWebException{
		
		 if(id != null){
			 entity = flowEngine.getWfProcessInstanceDao().load(id);
			 entity.setId(null);
		 }
		 processDefList = flowEngine.getWfProcessDefinitionDao().getMaxVerList();
		 return NEW_EDIT;
	 }
	 
	 /**
	  * 进入修改
	  * @return
	  * @throws Exception
	  */
	 public String enterEdit() throws WfWebException{
		 
		 entity = flowEngine.getWfProcessInstanceDao().load(id);
		 return NEW_EDIT;
	 }
	 
	 /**
	  * 进入查看
	  * @return
	  * @throws Exception
	  */
	 public String enterView() throws WfWebException{
		 
		 form = flowEngine.getWfProcessInstanceDao().loadForm(id);
		 
		 WfVariableInstanceSearch varInstSearch = new WfVariableInstanceSearch();
		 varInstSearch.setProcessInstId(form.getId());
		 List<WfVariableInstance> variableInstList = flowEngine.getWfVariableInstanceDao()
		 .listAll(varInstSearch);
		 this.model.put("variableInstList", variableInstList);
		 
		 List<WfActivityInstanceForm> activityInstFormList = flowEngine.loadProcessInstance(form.getId())
		 .getActivityInstFormList();
		 this.model.put("activityInstFormList", activityInstFormList);
		 
		 return VIEW;
	 }
	 
	 /**
	  * 发起流程实例
	  * @return
	  * @throws Exception
	  */
	 public String start() throws WfWebException{

		 if(id != null){
			 return update();
		 }
		 searchEntity = getSearchEntity();
		 
		 //获得流程变量
		 Map<String,Object> varMap = new HashMap<String, Object>();
		 try {
			 List<String> varKeyList = searchEntity.getVarKey();
			 List<String> varValueList = searchEntity.getVarValue();
			 if(varKeyList != null){
				 for(int i = 0; i < varKeyList.size(); i++){
					 String varKey = varKeyList.get(i);
					 String varValue = varValueList.get(i);
					 varMap.put(varKey, varValue);
				 }
			 }
		} catch (Exception e) {
			this.addErrMsg("获得流程变量失败");
			return NEW_EDIT;
		}
		
		 //key
		 String key = searchEntity.getKey();
		 
		 //用户ID
		 Long startUserId = getUserId();
		 
		 //数据校验
		 if(StringUtils.isEmpty(key) || startUserId == null){
			 this.addErrMsg("key 或 startUserId 不能为空");
			 return NEW_EDIT;
		 }
		 
		 //流程发起
		 ProcessInstance processInst = this.flowEngine.startProcessInstByKey(key,searchEntity.getName(),varMap,startUserId);
		 
		 //起草活动发起
		 processInst.startActivityInstance();
		 
		 return getRedirect(LIST);
	 }
	 
	 /**
	  * 修改
	  * @return
	  * @throws Exception
	  */
	 public String update() throws WfWebException{
		 
		 entity = getEntity();
		 return getRedirect(LIST);
	 }
	 
	/**
	 * 流程实例暂停
	 * @return
	 * @throws Exception
	 */
	public String pause() throws WfWebException{
		
		ProcessInstance pi = flowEngine.loadProcessInstance(id).pause();
		flowEngine.submit(pi);
		return getRedirect(LIST);
	}
	
	/**
	 * 流程实例恢复
	 * @return
	 * @throws Exception
	 */
	public String resume() throws WfWebException{
		
		ProcessInstance pi = flowEngine.loadProcessInstance(id).resume();
		flowEngine.submit(pi);
		return getRedirect(LIST);
		
	}
	
	/**
	 * 流程实例结束
	 * @return
	 * @throws Exception
	 */
	public String end() throws WfWebException{
		
		ProcessInstance pi = flowEngine.loadProcessInstance(id).end();
		flowEngine.submit(pi);
		return getRedirect(LIST);
	}
	 
	 
	 /**
	  * 删除
	  * @return
	  * @throws Exception
	  */
	 public String delete() throws WfWebException{
		 
		 Collection<Serializable> idList = getIdList();
		 if(idList != null){
			flowEngine.getWfProcessInstanceDao().deleteAllById(idList);
		 }
		 return getRedirect(LIST);
	 }
	 
	 
	@WzFunAnnotation(act = ConstantUtils.EXEC_AJAX)
	public List<WfProcessDefinition> getProcessDefList() {
		
		processDefList = flowEngine.getWfProcessDefinitionDao().getMaxVerList();
		return processDefList;
	}
	
	@WzFunAnnotation(act = ConstantUtils.EXEC_AJAX)
	public List<WfDataDictionary> getStateList() {
		
		stateList = flowEngine.getWfDataDictionaryDao().listByParentId(1L);
		return stateList;
	}

	 
	
}
